import re
import os
import shutil
import requests
import json
from loguru import logger
import time

class UserEmail:
    def __init__(self, email=""):
        self.session = requests.Session()
        self.session.headers = {
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
            "application-name": "web",
            "application-version": "4.0.0",
            "content-type": "application/json",
            "origin": "https://temp-mail.io",
            "priority": "u=1, i",
            "referer": "https://temp-mail.io/",
            "sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Microsoft Edge\";v=\"133\", \"Chromium\";v=\"133\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-site",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0",
            "x-cors-header": "iaWg3pchvFx48fY"
        }
        self.email = email
        self.code = ""

    def getemial(self):
        url = "https://api.internal.temp-mail.io/api/v3/email/new"
        data = {
            "min_name_length": 10,
            "max_name_length": 10
        }
        data = json.dumps(data, separators=(',', ':'))
        response = self.session.post(url, data=data)
        self.email = response.json().get("email")
        logger.success(f"成功获取邮箱：{self.email}")
        return self.email

    def message(self):
        url = f'https://api.internal.temp-mail.io/api/v3/email/{self.email}/messages'
        resp = self.session.get(url)
        if len(resp.json()) > 0:
            codetxt = resp.json()[0].get("body_text")
            # 正则过滤
            codetxt = codetxt.replace("\n", "")
            code = re.findall(r"your open browser window(.*?)This code", codetxt, re.S)[0]
            self.code = code.replace('.', '')
            logger.success(f"成功解析验证码：{code}")
            return self.code
        else:
            return False

def windows():
    # 获取用户windows用户变量
    # 查找某个应用程序的路径
    app_name = "Cursor"  # 你可以替换成任何应用程序的名称
    app_path = shutil.which(app_name)

    if app_path:
        logger.info("cursor路径：{}".format(app_path))
    else:
        logger.error("请检查是否配置Cursor用户变量")
        return False
    path2 = app_path.split("app\\bin")[0] + "\\" + "app"
    # 分别读取
    return path2
from dotenv import load_dotenv
import os

# 加载 .env 文件中的变量
load_dotenv()

# 获取单个环境变量
database_url = os.getenv("BROWSER_PATH")


# 输出读取的值
print(f"DATABASE_URL: {database_url}")


# 获取默认浏览器路径



if __name__ == '__main__':
    windows()
